package project.active;

import java.util.ArrayList;
import java.util.List;

import model.pojo.Employee;
import project.dao.EmployeeDAO;
import project.dao.ProjectDAO;
import project.dao.TestProjectDAO;
import project.model.PrProject;
import project.model.PrTestProject;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import approve.action.R;

public class Lverrorlist extends Activity {
	Bundle language;
	String lang="EN",database="admin";
	PrProject pr =new PrProject();
	ProjectDAO prodao=new ProjectDAO();
	EmployeeDAO emdao=new EmployeeDAO();
	TestProjectDAO errdao= new TestProjectDAO();
	Employee emp=new Employee();
	TextView logout,username;
	TextView proinfo,title;
	ListView lstErr;
	Button btGoProMenu;
	PrTestProject err;
	ArrayList<PrTestProject> lst=new ArrayList<PrTestProject>();
	
	L_list_lsterr ader;
	
	@SuppressWarnings("rawtypes")
	List map = null;
	int count = 0;
	LinearLayout listPages;
	TextView txt_first, txt_last,txt_prev,txt_next;
	ImageView img_next, img_prev;
	int pageSize = 4;
	int pageCount = 0;
	int maxPages = 2;
	int currentPage = 1;
	List<Integer> listCurrentPages = new ArrayList<Integer>();
	
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lverrorlist);
		language = getIntent().getExtras().getBundle(getLang());
		
		Intent ii=this.getIntent();
		pr=(PrProject)ii.getSerializableExtra("project");		
		emp=(Employee)ii.getSerializableExtra("em");
		
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));
		
		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		title=(TextView)findViewById(R.id.l_tv_title_errcodelist);
		title.setText(language.getString("l_errinfo"));
		btGoProMenu = (Button)findViewById(R.id.lbt_ErrListToProMenu);
		lstErr=(ListView)findViewById(R.id.l_lv_ErrList);
		proinfo=(TextView)findViewById(R.id.l_tv_proinfoErr);
		btGoProMenu.setText(language.getString("project_info"));
		
		txt_prev=(TextView) findViewById(R.id.txt_prev);txt_prev.setText("<<");
		txt_next=(TextView) findViewById(R.id.txt_next);txt_next.setText(">>");		
		
		map=getListErrPG(pageSize, currentPage,pr.getIdProject(),getDatabase(),getLang());
		
		if (map != null && map.size() > 1) {
			lst = (ArrayList<PrTestProject>) map.get(0);
			count = (Integer) map.get(1);
		}
		
		proinfo.setText(pr.getIdProject()+" - "+pr.getNameProject());
		if(lst!=null && lst.size()>0){
			//lstErr.setAdapter(new ArrayAdapter<PrTestProject>(this,android.R.layout.simple_expandable_list_item_1,lst));
			ader=new L_list_lsterr(this, lst, language.getString("error"));
			lstErr.setAdapter(ader);
		}
		
		btGoProMenu.setOnClickListener(goMenuPro);
		lstErr.setOnItemClickListener(goErrDetail);
		
		pageCount = (int) Math.ceil((double) count / pageSize);
		
		if (pageCount > 0) {
			txt_first = (TextView) findViewById(R.id.txt_first);
			if (currentPage != 1) {
				txt_first.setTextColor(Color.BLACK);
				txt_first.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = 1;
						onCreate(savedInstanceState);

					}
				});
			}
			txt_last = (TextView) findViewById(R.id.txt_last);
			if (currentPage != pageCount) {
				txt_last.setTextColor(Color.BLACK);
				txt_last.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = pageCount;
						onCreate(savedInstanceState);

					}
				});
			}
			
			
			listPages = (LinearLayout) findViewById(R.id.listpages);
			listCurrentPages.clear();
			final int secIndex = (int) Math.ceil((double) currentPage
					/ maxPages);
			//image next prev
			if (currentPage > maxPages) {				
				txt_prev.setTextColor(Color.BLACK);
				txt_prev.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex - 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});

			}

			if (currentPage < pageCount) {
				txt_next.setTextColor(Color.BLACK);
				
				txt_next.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex + 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});

			}
			//list pages
			int maxIndex = secIndex * maxPages;
			int minIndex = maxIndex - (maxPages - 1);

			if (maxIndex <= pageCount) {
				for (int i = minIndex; i <= maxIndex; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}
					tv.setPadding(15, 0, 15, 0);

					listPages.addView(tv);
					listCurrentPages.add(i);
				}
			} else {
				for (int i = minIndex; i <= pageCount; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}
					tv.setPadding(15, 0, 15, 0);

					listPages.addView(tv);
					listCurrentPages.add(i);
				}
			}
			
		}
		
		
		
	}
	
//	public ArrayList<PrTestProject> getListErr(String idpro,String db,String idlang){
//		ArrayList<PrTestProject> lst = new ArrayList<PrTestProject>();
//		lst = errdao.loadListErr(idpro, db, idlang);
//		return lst;
//	}
	
	@SuppressWarnings({ "rawtypes", "static-access" })
	public List getListErrPG(int limit, int pageN,String idpro,String db,String idlang){		 
		return errdao.loadListErrPG(limit,pageN,idpro, db, idlang);
	}
	
	private OnClickListener goMenuPro = new OnClickListener() {
		
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lverrorlist.this, LSubprojectlistmenu.class);
			i.putExtra("em",emp);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("project", pr);
			i.putExtras(getIntent().getExtras());
			startActivity(i);
		}
	};
	
	private OnItemClickListener goErrDetail =  new OnItemClickListener() {

	
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			// TODO Auto-generated method stub
			err=(PrTestProject)lstErr.getItemAtPosition(position);
			Intent i=new Intent(Lverrorlist.this, Lerrdetail.class);
			System.out.println("aaa: "+err.getIdTest());	
			i.putExtras(getIntent().getExtras());
			i.putExtra("project",pr);
			i.putExtra("oneerr", err);
			i.putExtra("lang", getLang());
			i.putExtra("database", getDatabase());
			i.putExtra("em",emp);		
			
			startActivity(i);
		}
	};
	
	private OnClickListener listenerLogout=new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent i=new Intent(Lverrorlist.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
}
